<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Deposit extends MY_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->library(array('session', 'account_func'));

		$this->_autoloadModel();
		
		$wallets =  $this->getWalletsData();
		$currencies = $this->model->get_currencies();
		$this->template->set("wallets", $wallets);
		$this->template->set("av_currencies", $currencies);
		$balance = $this->calc_balance();
		if($balance){
			$cID = $balance["FCODE"];
			$res = $this->model->get_currency($cID);
			$cSign = $res['C_SIGN'];
			$balance_ammount = $balance["BALANCE"];
		}
		else{
			$balance_ammount = 0;	
			$cSign = "$";
		}
		$this->template->set("balance", $balance_ammount);
		$this->template->set("cSign", $cSign);
		
	}
	
	private function _autoloadModel(){
		$this->load->database('default', TRUE);
		$user_type = $this->session->userdata("USER_TYPE");
		//print "user_type:" . $user_type;
		switch ($user_type) {
			case "EW":{
				$this->load->database('ewallet', TRUE);
				$this->load->model("ewallet_model", "model", TRUE);
				break;
			}

			case "IP":{
				$this->load->database('payment', TRUE);
				$this->load->model("payments_model", "model", TRUE);
				break;
			}
		}
	}

	function index(){
		redirect(site_url("user/account/operations/deposit"));
	}
	
	function ecurrency($ps = NULL){
		$this->template->set("title", "My Account");
		$this->template->set("section", "Operations");
		$this->template->set("subSection", $this->account_func->subSectionName("ecurrency"));
		$data['ps_title'] = $this->account_func->ps_string($ps);
		$data['logged'] = $this->logged;
		$data['submenus'] = $this->sub_menus;
		switch ($ps) {
			case "perfectmoney": {
				$action = $this->uri->segment(5);
				$data['ps'] = "perfectmoney";
				switch ($action) {
					case "save":{
						$this->depositPMSave();
						break;
					}
					
					default: {
						$this->load->helper('security');
						$data['date'] = date("Y-m-d H:i:s");
						$code = do_hash(do_hash($this->session->userdata('USER_ID') . $data['date']), 'md5');
						$data['code'] = $code;
						$this->deposit_ecurrency_form_view($data);
						break;
					}
				}
				break;
			}
			default: {
				redirect(site_url("user/account/operations/deposit"));
				break;
			}
		}
	}
	
	
	/*start of view functions section*/
	private function deposit_ecurrency_form_view($data){
		$data['wallets'] = $this->getWalletsData();
		$this->load->database('default', TRUE, TRUE);
		$this->session->set_userdata('confirm_code', $data['code']);
		$this->session->set_userdata('gen_date', $data['date']);
		$this->template->load("user/layout_account", "user/deposit_ecurrency_form_view", $data);
	}

	/*end of view functions*/
	
	
	
	private function calc_balance() {
		$balance = array("FCODE" => 5, "BALANCE" => 0);
		$balance = $this->model->get_balance($this->session->userdata("ID"));
		
		return $balance;
	}
	
	
	/**
	 * 
	 * Returns the specific data for the user current wallet
	 * @return array with wallet data  
	 */
	protected function getWalletData($walletCode) {
		$user_id = $this->session->userdata("ID");
		$wallet = $this->model->get_wallet($user_id, $walletCode);
		if($wallet){
			$wallet['currencies'] = $this->model->get_wcurrencies($wallet['ID']);
			return $wallet;
		}
		else{
			return false;
		}
	}
	
	protected function getWalletsData() {
		$user_id = $this->session->userdata("ID");
		$wallets = $this->model->get_wallets($user_id);
		$res_wallets = array();
		foreach ($wallets as $key => $val) {
			foreach ($wallets[$key] as $key1 => $val1) {
				$res_wallets[$key][$key1] = $val1;
			}
			$currencies = $this->model->get_wcurrencies($res_wallets[$key]['ID']);
			$res_wallets[$key]['currencies'] = $currencies;
		}
		
		return $res_wallets;
	}
	
	
	private function depositPMSave(){
		$this->load->library(array('form_validation'));
		$this->form_validation->set_rules('ammount', 'Ammount', 'required|callback__check_ammount');
		$this->form_validation->set_rules('depositWallet', 'Wallet', 'required');
		//$this->form_validation->set_rules('depositCurrency', 'Currency', 'required');
		$ps = $this->uri->segment(4);
		if ($this->form_validation->run() == FALSE){
			$data['error']['ammount'] = strip_tags(form_error('ammount'));
			$data['error']['depositWallet'] = strip_tags(form_error('depositWallet'));
			//$data['error']['depositCurrency'] = strip_tags(form_error('depositCurrency'));
			$data['error']['message'] = "Some information is incorrect or missing. Please correct your entries and try again.";
			$data['link'] = site_url("user/deposit/ecurrency/{$ps}#message");
       }
       else{
       	$code_obj = new Confirm_code(); 
       	$code_obj->ACCOUNT_ID = (int)$this->session->userdata("ID");
       	$code_obj->AMMOUNT = $this->input->post("ammount");
       	$code_obj->CODE = $this->session->userdata("confirm_code");
       	$code_obj->GEN_DATE = $this->session->userdata("gen_date");
       	$code_obj->PAY_SYSTEM = $ps;
       	$code_obj->STATUS_CODE = 2;
       	
       	$this->load->database('default', TRUE, TRUE);
       	$code = $this->session->userdata("confirm_code");
       	$code = "1234567890";
       	
       	$this->load->model("kashmart_model");
       	$payments = $this->kashmart_model->getPayment($code);
       	if ($payments && (count($payments) == 1)){
       		$code_obj->SENDER_REFERENCE = $payments['Sender_Reference'];
       	}	
       	$this->_autoloadModel();
       	$res = $this->model->log_confirm_code($code_obj);
       	if($res){
       		$data['insert_id'] = $res; 
       		$data['success'] = "Request sent. Waiting payment confirmation."; 
       		$data['redirect'] = site_url("user/account/operations/deposit"); 
       		$data['delay'] = 5; 
       	}
       	else {
       		$data['error']['message'] = "DB Error";
       	}
       	
       	$data['code'] = $code;
       	$this->load->database('default', TRUE, TRUE);
       	//$arr = array('convertFrom' => '', 'convertTo' => '', 'ammount' => '');
       	//$this->session->unset_userdata($arr);
       	//$data['redirect'] = site_url("user/account/operations/exchange/confirm");
       }
       echo json_encode($data);
	}	
		
		
	/*check functions region*/

	function _check_ammount($number){
		if((is_numeric($number)) && (round($number, 2) > 0)){
			return TRUE;
		}
		else{
			$err = "You have entered an invalid amount. (e.g. 5.00)";
			$this->form_validation->set_message("_check_ammount", $err);
			return FALSE;
		}
	}
	
	/*end of check functions region*/
	
	
	
	/*other types of utility functions region*/
	
	/*end of other types of utility functions*/
}
